package net.gz01.wallet.web.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.jfinal.plugin.activerecord.Page;

import net.gz01.wallet.common.ZOIDGenerate;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.common.ZOSqlHelper;
import net.gz01.wallet.web.config.SystemConfig;
import net.gz01.wallet.web.config.SystemEnum.BizType;
import net.gz01.wallet.web.model.base.BaseTradeConfig;

@SuppressWarnings("serial")
public class TradeConfig extends BaseTradeConfig<TradeConfig>{

	private static Map<String, List<TradeConfig>> listMap = new HashMap<>();
	public static final TradeConfig dao = new TradeConfig().dao();

	public Page<TradeConfig> search(ZOSearchCondition sc){
		int pageNumber = sc.page.getPageNum();
		int pageSize = sc.page.getNumPerPage();

		ZOSqlHelper sql = new ZOSqlHelper(" from (select tc.*,c.name as channelName,m.name as merchName,f.name as feeName from trade_config tc");
		sql.append(" join bd_channel c on (tc.channelCode = c.code) join bd_merch m on (tc.merchCode = m.code) ");
		sql.append(" join bd_fee_model f on (tc.feeId = f.id) ) tmp");
		sql.equals("merchCode", sc.get("merchCode"));
		sql.like("channelCode", sc.get("channelCode"));
		sql.orderBy("channelCode", "desc");
		return super.paginate(pageNumber, pageSize, "select *", sql.toString(),sql.getParams());
	}

	public TradeConfig select(String merchCode, String type) {
		return super.findFirst("select * from trade_config where merchCode = ? and bizType = ?", merchCode, type);
	}

	public TradeConfig select(String merchCode,String channelCode,String type) {
		return super.findFirst("select * from trade_config where merchCode = ? and channelCode = ? and bizType = ?",merchCode,channelCode,type);
	}

	public TradeConfig selectCharge(String merchCode) {
		return super.findFirst("select * from trade_config where merchCode = ? and bizType = ?",merchCode, BizType.Charge.getValue());
	}

	public List<TradeConfig> select(String merchCode) {
		return super.find("select * from trade_config where merchCode = ?",merchCode);
	}

	public String getFeeName() {
		return FeeModel.dao.findById(getFeeId()).getName();
	}

	public String getRiskRuleName() {
		return RiskRule.dao.findById(getRiskRuleId()).getName();
	}

	public String getBizTypeName() {
		return Dictionary.DICTIONARY_MAP.get(getBizType());
	}

	public void autoCreate(String merchCode){
		List<TradeConfig> tradeConfigList = null;
		String defaultCode =  SystemConfig.config.get("default_merchCode");
		if(listMap!=null && !listMap.isEmpty()){
			tradeConfigList = listMap.get(defaultCode);
		}
		if(tradeConfigList!=null){

		}else {
			tradeConfigList = TradeConfig.dao.select(defaultCode);
			listMap.put(defaultCode, tradeConfigList);
		}
		for(int i = 0; i < tradeConfigList.size(); i++){
			String string = JSON.toJSONString(tradeConfigList.get(i));
			TradeConfig config = JSON.parseObject(string, TradeConfig.class);
			config.setId(ZOIDGenerate.getUUID());
			config.setMerchCode(merchCode);
			config.save();
		}

//		SystemEnum.BizType[] values = SystemEnum.BizType.values();
//		for (SystemEnum.BizType bizType : values) {
//			TradeConfig tradeConfig = new TradeConfig();
//			tradeConfig.setId(ZOIDGenerate.getUUID());
//			tradeConfig.setBizType(bizType.getValue());
//			tradeConfig.setChannelCode(Channel.dao.selectAll().get(0).getCode());
//			tradeConfig.setFeeId(FeeModel.dao.selectBybizType(bizType.getValue()).getId());
//			tradeConfig.setMerchCode(merchCode);
//			tradeConfig.setRiskRuleId(RiskRule.dao.selectAll().get(0).getId());
//			tradeConfig.save();
//		}
	}

	public Boolean isExistByChannelCode(String channelCode){
		if(super.findFirst("select * from trade_config where channelCode = ?",channelCode) != null){
			return true;
		}else{
			return false;
		}
	}

	public Boolean isExistByRiskRuleId(String riskRuleId){
		if(super.findFirst("select * from trade_config where riskRuleId = ?",riskRuleId) != null){
			return true;
		}else{
			return false;
		}
	}

	public Boolean isExistByFeeId(String feeId){
		if(super.findFirst("select * from trade_config where feeId = ?",feeId) != null){
			return true;
		}else{
			return false;
		}
	}
}
